//控制层
app.controller('goodsController', function ($scope, $controller, goodsService,itemCatService) {

    $controller('baseController', {$scope: $scope});//继承

    //读取列表数据绑定到表单中  
    $scope.findAll = function () {
        goodsService.findAll().success(
            function (response) {
                $scope.list = response;
            }
        );
    }

    //分页
    $scope.findPage = function (page, rows) {
        goodsService.findPage(page, rows).success(
            function (response) {
                $scope.list = response.rows;
                $scope.paginationConf.totalItems = response.total;//更新总记录数
            }
        );
    }

    //查询实体
    $scope.findOne = function (id) {
        goodsService.findOne(id).success(
            function (response) {
                $scope.entity = response;
            }
        );
    }

    //保存
    $scope.save = function () {
        var serviceObject;//服务层对象
        if ($scope.entity.id != null) {//如果有ID
            serviceObject = goodsService.update($scope.entity); //修改
        } else {
            serviceObject = goodsService.add($scope.entity);//增加
        }
        serviceObject.success(
            function (response) {
                if (response.success) {
                    //重新查询
                    $scope.reloadList();//重新加载
                } else {
                    alert(response.message);
                }
            }
        );
    }

    $scope.status=['未审核','已审核','审核未通过','已关闭'];

    /*//保存
    $scope.add = function () {
        goodsService.success(
            function (response) {
                if (response.success) {
                    //重新查询
                    $scope.entity = {};//重新加载
                } else {
                    alert(response.message);
                }
            }
        );
    }*/


    //批量删除
    $scope.dele = function () {
        //获取选中的复选框
        goodsService.dele($scope.selectIds).success(
            function (response) {
                if (response.success) {
                    $scope.reloadList();//刷新列表
                    $scope.selectIds = [];
                }
            }
        );
    }

    $scope.searchEntity = {};//定义搜索对象

    //搜索
    $scope.search = function (page, rows) {
        goodsService.search(page, rows, $scope.searchEntity).success(
            function (response) {
                $scope.list = response.rows;
                $scope.paginationConf.totalItems = response.total;//更新总记录数
            }
        );
    }

    $scope.itemCatList=[];//商品分类列表
    //查询商品分类列表
    $scope.findItemCatList=function(){
        itemCatService.findAll().success(
            function(response){
                for(var i=0;i<response.length;i++){
                    //itemCatList中元素的下标为id，因为商品列表是以商品的id来排序的，一级分类也是包含在商品信息中的，所以itemList的元素和商品信息保持一致
                    $scope.itemCatList[response[i].id]=response[i].name;
                }
            }
        );
    }


    //				 0		1		2		3
    $scope.status=['未审核','已审核','审核未通过','已关闭'];

    $scope.itemCatList=[];//商品分类列表
    //查询商品分类列表
    $scope.findItemCatList=function(){
        itemCatService.findAll().success(
            function(response){
                for(var i=0;i<response.length;i++){
                    //itemCatList中元素的下标为id，因为商品列表是以商品的id来排序的，一级分类也是包含在商品信息中的，所以itemList的元素和商品信息保持一致
                    $scope.itemCatList[response[i].id]=response[i].name; //$scope.itemCatList[865]=手机
                }
            }
        );
    }

    $scope.updateStatus=function (status) {
        goodsService.updateStatus($scope.selectIds,status).success(
            function (data) {
                if(data.success){
                    $scope.reloadList();
                    $scope.selectIds=[];
                }else {
                    alert(data.message);
                }
            }
        );
    }


});	
